#pragma region Include
#include <Windows.h>
#include <string>
#include <list>

#include "file.h"
#pragma endregion Include

CFile::CFile()
	:cur_path("")
{
}

CFile::~CFile()
{}

list<string> CFile::GetListDrives()
{
	DWORD dwDrives = 0;
	DWORD count = 1;
	char ch[3] = "A:";

	dwDrives = GetLogicalDrives();
	for(int i = 0; i<26; i++)
	{
		if(dwDrives&count)
		{
			ch[0] = 0x41+i;
			ch[1] = ':';
			list_drives.push_back(ch);
		}

		count *= 2;
	}

	return list_drives;
}

list<CFile::RECORD> CFile::GetListFiles(const string path)
{
	if(!path.empty())
		cur_path.assign(path);

	HANDLE hFile = NULL;
	WIN32_FIND_DATA fd;
	bool result = false;

	RECORD rec;

	string search_path = cur_path + "\\*.*";
	
	hFile = FindFirstFile(search_path.c_str(), &fd);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		FindClose(hFile);

		return list_files;
	}

	rec.file_name = fd.cFileName;
	rec.f_dir = fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY;
	list_files.push_back(rec);

	do
	{
		result = FindNextFile(hFile, &fd);

		if(result)
		{
			rec.file_name = fd.cFileName;
			rec.f_dir = fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY;
			list_files.push_back(rec);
		}
	}
	while(result);

	del_dots_from_cur_path();

	return list_files;
}

string CFile::GenHtmlListDrivesPage(const string srv_address)
{
	string http_path;
	string::size_type pos;

	GetListDrives();

	html_page = "<html><head></head><body>";

	list<string>::iterator it;

	html_page += "<a href='http://" + srv_address + "/index.html'>To SCREEN</a>";

	html_page += "<table>";
	for(it = list_drives.begin(); it != list_drives.end(); it++)
	{
		html_page += "<tr><td><a href='http://" + srv_address + 
			"/files.html?cmd="+ (*it) +"'> " + (*it) + "</a></td></tr>";
	}
	html_page += "</table>";

	html_page += "</body></html>";

	return html_page;
}

string CFile::GenHtmlListFilesPage(const string srv_address)
{
	string http_path;
	string::size_type pos;

	http_path = cur_path;

	while(1)
	{
		pos = http_path.find("\\");
		if(pos == string::npos)
			break;
		http_path.replace(pos, 1, "/");
	}

	html_page = "<html><head>";
	html_page += "<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>";
	html_page += "</head><body>";
	html_page += "<p><a href='http://" + srv_address + "/index.html'>To SCREEN</a></p>";
	html_page += "<p><a href='http://" + srv_address + "/files.html?cmd='>To Drives</a></p>";
	
	list<RECORD>::iterator it;

	html_page += "<table>";
	for(it = list_files.begin(); it != list_files.end(); it++)
	{
		if((*it).f_dir == false)
			html_page += "<tr><td><a href='http://" + srv_address + 
				"/cmd/"+ http_path + "/" + (*it).file_name +"'> " + 
				(*it).file_name + "</a></td></tr>";
		else
			html_page += "<tr><td><a href='http://" + srv_address + 
				"/files.html?cmd="+ http_path + "/" + 
				(*it).file_name +"'> " + (*it).file_name + "</a></td></tr>";
	}
	html_page += "</table>";

	html_page += "</body></html>";

	return html_page;
}

void CFile::SaveHtmlListFilesPageToFile(const string file_name)
{
	html_file_name = file_name;

	FILE *fp = fopen(html_file_name.c_str(), "w");

	fprintf(fp, "%s", html_page.c_str());

	fclose(fp);
}

bool CFile::IsDir(const string path)
{
	HANDLE hFile = NULL;
	WIN32_FIND_DATA fd;
	bool f_dir = false;

	if(path.find("\\..") != string::npos)
	{
		f_dir = true;

		return f_dir;
	}

	hFile = FindFirstFile(path.c_str(), &fd);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		FindClose(hFile);

		return false;
	}

	f_dir = fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY;

	return f_dir;
}

string CFile::HttpPathToWinPath(string &http_path)
{
	string::size_type pos;

	while(1)
	{
		pos = http_path.find("/");
		if(pos == string::npos)
			break;
		http_path.replace(pos, 1, "\\");
	}

	return http_path;
}

void CFile::del_dots_from_cur_path()
{
	int len = cur_path.length();
	string::size_type pos = 0;

	pos = cur_path.find("\\..");
	if(pos != string::npos)
	{
		cur_path.replace(pos, 3, "");

		len = cur_path.length();
		pos = cur_path.find_last_of("\\");

		cur_path.replace(pos, len - pos, "");
		return;
	}
	
	pos = cur_path.find("\\.");
	if(pos != string::npos)
		cur_path.replace(pos, 2, "");

	return;
}